package com.huluobo.high3demo.adapter;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.high3demo.R;
import com.huluobo.mvvm.baen.VideoBeanResult;
import com.huluobo.mvvm.utils.ImageLoader;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

/**
 * 多布局万能适配器
 * Created by LC on 2023/9/21.
 */
public class MutableReCommendAdapter extends BaseMultiItemQuickAdapter<VideoBeanResult.VideoBean, BaseViewHolder> {
    //控制多布局根据type来决定使用哪个布局
    public MutableReCommendAdapter() {
        addItemType(0, R.layout.item_video);//普通视频
        addItemType(1, R.layout.item_video_live);//直播视频
        addItemType(2, R.layout.item_video_full);//全屏视频
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        StandardGSYVideoPlayer player = baseViewHolder.getView(R.id.video_gsy_player);
        player.setUp(videoBean.getVideopath(), true, "");
        //播放器上的返回按钮
        player.getBackButton().setVisibility(View.GONE);

        //根绝不同的类型设置赋值
        switch (videoBean.getType()) {
            case 0:
            case 2:
                setVideoOptions(baseViewHolder, videoBean);
                break;
            case 1:
                setLiveOptions(baseViewHolder, videoBean);
                break;
        }
    }

    /**
     * 设置直播操作(进入直播间)
     *
     * @param baseViewHolder
     * @param videoBean
     */
    private void setLiveOptions(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        //设置公共视频信息(左下角)
        setCommonVideoInfo(baseViewHolder, videoBean);
    }

    /**
     * 设置视频操作(点赞,评论,关注,分享,收藏)
     *
     * @param baseViewHolder
     * @param videoBean
     */
    private void setVideoOptions(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        //设置公共视频信息(左下角)
        setCommonVideoInfo(baseViewHolder, videoBean);
        //右下区域赋值
        //头像
        ImageView headView = baseViewHolder.getView(R.id.video_tv_head);
        ImageLoader.getInstance().loadCircleImage(getContext(), videoBean.getHeadpath(), headView);
        //关注状态,0代表未关注,1代表已关住
        if (videoBean.getGuanzhu() == 0) {
            baseViewHolder.getView(R.id.video_iv_add).setVisibility(View.VISIBLE);
        } else {
            baseViewHolder.getView(R.id.video_iv_add).setVisibility(View.GONE);
        }

        //点赞状态,0代表未点赞,1代表已点赞
        if (videoBean.getDianzan() == 0) {
            baseViewHolder.setImageResource(R.id.video_iv_like_btn, R.drawable.ic_xihuan);
        } else {
            baseViewHolder.setImageResource(R.id.video_iv_like_btn, R.drawable.ic_xihuanred);
        }

        //点赞数量
        int likeCount = videoBean.getLike_count() | 12000;
        baseViewHolder.setText(R.id.video_tv_like_count, likeCount < 1000 ? String.valueOf(likeCount) :
                likeCount / 10000 + "W");

        //评论数量
        baseViewHolder.setText(R.id.video_tv_comment_count, "1.1W");
        //收藏数量
        baseViewHolder.setText(R.id.video_tv_follow_count, "1.1W");
        //分享数量
        baseViewHolder.setText(R.id.video_tv_share_count, "1.1W");

        //碟片动画
        ImageView videoPlayer = baseViewHolder.getView(R.id.video_iv_music_player);
        ImageLoader.getInstance().loadCircleImage(getContext(), videoBean.getHeadpath(), videoPlayer);

        ObjectAnimator animator = ObjectAnimator.ofFloat(videoPlayer, "Rotation", 0, 360);
        animator.setDuration(3000);
        animator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
        animator.setInterpolator(new LinearInterpolator());//匀速的插值器
        animator.start();
    }

    private void setCommonVideoInfo(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        //左下区域信息赋值
        //地址
        baseViewHolder.setText(R.id.video_tv_address, videoBean.getAddress());
        //标题
        baseViewHolder.setText(R.id.video_tv_username, videoBean.getAuthname());
        //描述
        baseViewHolder.setText(R.id.video_tv_caption, videoBean.getCaption());
        //原创
        TextView tvPao = baseViewHolder.getView(R.id.video_tv_pao);
        tvPao.setText(videoBean.getAuthname() + "原创作品");
        //跑马灯需要获取焦点
        tvPao.requestFocus();
    }
}
